(function($) {
	
	$.controller.album = {
			index: function(actionVars) {
				$.widget.photoList.preparePhotoList();
			},
			load: function (actionVars) {
				this.loadAlbum(actionVars);
			},
			loadAlbum: function (actionVars) {
				//Only load album if is not loaded
				if(actionVars.id != $.widget.photoList.widget.find(".thumbs").attr("album"))
					try{
						$.widget.albumList.loadAlbum(actionVars.id,1);
					}catch(err){
						jAlert("Error while loading album: "+err.message);
					}
				else if(actionVars.page != null){
					try{
						$.widget.albumList.loadAlbum(actionVars.id,actionVars.page);
					}catch(err){
						jAlert("Error while loading album: "+err.message);
					}
				}
				else
					$.widget.photoList.preparePhotoList();
				
				
			},
			loadPage: function(page){
				this.loadAlbum($.url.get("id"),page);
			},
			newComments: function(actionVars) {
				$.widget.photoList.widget.find("ul").html("");
				$.getJSON("core/user/getNewPhotoComments",function(data){
					$.each(data,function(i,photo){
						$.widget.photoList.insertPhoto({photo_id:photo.photo_id, title:photo.count+" new comments"});
					});
				});
			},
			
			newTags: function(actionVars) {
				$.widget.photoList.widget.find("ul").html("");
				$.getJSON("core/user/getNewPhotoTags",function(data){
					$.each(data,function(i,photo){
						$.widget.photoList.insertPhoto({photo_id:photo.photo_id, title:""});
					});
				});
			},
			loadPageTagged: function(page){
				$.controller.album.tagged({id:$.url.get("id"), page:page});
			},
			tagged: function(actionVars) {
				$.getJSON("core/album/getTaggedAlbum/id/"+actionVars.id+"/page/"+actionVars.page,function(data){
					if(data.error)
						location.href="#album";
					else{
						$.widget.photoList.widget.find(".paginator").text(data.photos.pagecount);
						var photoList = $('<ul class="thumbs" album="tagged">');
						$.each(data.photos.photos,function(i,photo){
							photoList.append('<li class="ui-corner-all ui-state-default" photo="'+photo.photo_id+'">'+
							'<a href="#photo/loadPhoto/id/'+photo.photo_id+'"><img style="height: 115px;"src="/core.php/photo/thumbnail/id/'+photo.photo_id+'/type/list"/></a>'+
						'</li>');
						});
						try{
							$.widget.photoList.widget.find(".title").text(data.album.name);
							$.widget.photoList.widget.find(".photos").html(photoList)
							.find("img")
							.addClass("ui-state-default ui-corner-all")
							.lazyload({
							    placeholder : "images/progress.gif",
							    effect : "fadeIn"
							});
						
						}catch(err){
							jAlert($._("Dependency missing")+": "+err.message);
						}
						if(actionVars.page == null)
							page=1;
						else
							page=actionVars.page;
						$.widget.photoList.widget.find(".paginator").pager({ pagenumber: page, pagecount: data.photos.pagecount, buttonClickCallback: $.controller.album.loadPageTagged });
					}
				});
			}
			
		
	}
	
})(jQuery);